#define reset 0
#define work 1
#define cal 2			

#include "UART.h"
#include "ADC.h"
#include "CAL.h"
#include <stdio.h>
#include <ADUC841.H>
#include <math.h>

void CAL_start(){
  ECON=0x06;
}

void CAL_store(unsigned int a,unsigned int b){
  EADRH=0;
  EADRL=a*5+b;
	  ECON=0x01;
	  EDATA1=CAL_char_Temp[0];
	  ECON=0x05;
	  ECON=0x02;
	  ECON=0x04;
	  ECON=0x01;
	  EDATA2=CAL_char_Temp[1];
	  ECON=0x05;
	  ECON=0x02;
	  ECON=0x04;
	  ECON=0x01;
	  EDATA3=CAL_char_Temp[2];
	  ECON=0x05;
	  ECON=0x02;
	  ECON=0x04;
	  ECON=0x01;
	  EDATA4=CAL_char_Temp[3];
	  ECON=0x05;
	  ECON=0x02;
	  ECON=0x04;
  EADRH=0x01;
  EADRL=a*5+b;
	  ECON=0x01;
	  EDATA1=CAL_char_Temp[4];
	  ECON=0x05;
	  ECON=0x02;
	  ECON=0x04;
	  ECON=0x01;
	  EDATA2=CAL_char_Temp[5];
	  ECON=0x05;
	  ECON=0x02;
	  ECON=0x04;
	  ECON=0x01;
	  EDATA2=CAL_char_Temp[6];
	  ECON=0x05;
	  ECON=0x02;
	  ECON=0x04;
	  ECON=0x01;
	  EDATA2=CAL_char_Temp[7];
	  ECON=0x05;
	  ECON=0x02;
	  ECON=0x04;
}

void CAL_read(unsigned int a,unsigned int b){
  long int Cal_Temp,s,e,m;
  EADRH=0;
  EADRL=a*5+b;
  ECON=0x01;
  Cal_Temp=((long int)(EDATA1))+((long int)(EDATA2)<<8)+((long int)(EDATA3)<<16)+((long int)(EDATA4)<<24);
  s=((Cal_Temp>>31)==0)?1:-1;
  e=(Cal_Temp>>23)&0xff;
  if (e==0){
    m=((Cal_Temp&0x7fffff)<<1);
  }else{
    m=(Cal_Temp&0x7fffff)|0x800000;
  }
  CAL_flash_set_Data[a][b]=s*m*(float)pow(2,e-150);
  EADRH=0x01;
  EADRL=a*5+b;
  ECON=0X01;
  Cal_Temp=((long int)(EDATA1))+((long int)(EDATA2)<<8)+((long int)(EDATA3)<<16)+((long int)(EDATA4)<<24);
  s=((Cal_Temp>>31)==0)?1:-1;
  e=(Cal_Temp>>23)&0xff;
  if (e==0){
    m=((Cal_Temp&0x7fffff)<<1);
  }else{
    m=(Cal_Temp&0x7fffff)|0x800000;
  }
  CAL_flash_measure_Data[a][b]=s*m*(float)pow(2,e-150);
}

void CAL_send(unsigned int a,unsigned int b){

	  CAL_read(i,j);

	  sprintf(CAL_pc_set_char,"%d",CAL_flash_set_Data[a][b]);
	  UART_send(CAL_pc_set_char);
	  UART_send("s");
	  sprintf(CAL_pc_measure_char,"%d",CAL_flash_measure_Data[a][b]);
	  UART_send(CAL_pc_measure_char);
	  //UART_send("s");

  //UART_send("END");
}

void CAL_coe_store(unsigned int a,unsigned int b){
    EADRH=2;
    EADRL=a*5+b;
	  ECON=0x01;
	  EDATA1=CAL_coe_char_Temp[0];
	  ECON=0x05;
	  ECON=0x02;
	  ECON=0x04;
	  ECON=0x01;
	  EDATA2=CAL_coe_char_Temp[1];
	  ECON=0x05;
	  ECON=0x02;
	  ECON=0x04;
	  ECON=0x01;
	  EDATA3=CAL_coe_char_Temp[2];
	  ECON=0x05;
	  ECON=0x02;
	  ECON=0x04;
	  ECON=0x01;
	  EDATA4=CAL_coe_char_Temp[3];
	  ECON=0x05;
	  ECON=0x02;
	  ECON=0x04;
}

/*void CAL_coe_read(unsigned int a,unsigned int b){
  long int Coe_Temp,s,e,m;
  Coefficient[a][b]=0;;
  EADRH=2;
  EADRL=a*5+b;
  ECON=0x01;
  Coe_Temp=((long int)(EDATA1))+((long int)(EDATA2)<<8)+((long int)(EDATA3)<<16)+((long int)(EDATA4)<<24);
  s=((Coe_Temp>>31)==0)?1:-1;
  e=(Coe_Temp>>23)&0xff;
  if (e==0){
    m=((Coe_Temp&0x7fffff)<<1);
  }else{
    m=(Coe_Temp&0x7fffff)|0x800000;
  }
  Coefficient[a][b]=s*m*(float)pow(2,e-150);
}*/

void CAL_send_temp(unsigned int a){
   ADC_start();
   while (!f_newADC);
   ADC_stop();
   f_newADC = 0;
   sprintf(CAL_pc_set_char,"%d",ADCdata[a]);
   UART_send(CAL_pc_set_char);
}